package com.dms.system.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

import com.dms.system.domain.SysPlant;
import com.dms.system.domain.dto.SysPlantRecordDTO;
import com.dms.system.mapper.SysPlantMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dms.system.mapper.SysRecordMapper;
import com.dms.system.domain.SysRecord;
import com.dms.system.service.ISysRecordService;
import com.dms.common.core.text.Convert;

/**
 * 巡检记录，巡检人员上传现场数据Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-03-27
 */
@Service
public class SysRecordServiceImpl implements ISysRecordService 
{
    @Autowired
    private SysRecordMapper sysRecordMapper;

    @Autowired
    private SysPlantMapper sysPlantMapper;

    @Override
    public List<SysPlantRecordDTO> selectSysPlantRecordDTOList(SysRecord sysRecord,boolean isDay) {
        if(isDay){
            return sysRecordMapper.selectSysPlantRecordDTOListPerDay(sysRecord);
        }
        return sysRecordMapper.selectSysPlantRecordDTOListPerMinute(sysRecord);
    }

    @Override
    public int emulateInsertRecord() {
        Date date = new Date();
        // 获取有哪些设备
        SysPlant sysPlant = new SysPlant();
        sysPlant.setPlantStatus("1");
        List<SysPlant> plantList = sysPlantMapper.selectSysPlantList(sysPlant);
        List<SysRecord> recordList = new ArrayList<>();
        // 模拟读取设备传感器数据
        // 读取所有状态为正常的设备的温度和压力
        Random random = new Random();
        int temperature;
        int pressure;
        // 遍历设备数组，记录每个设备的温度和压力
        for(SysPlant plant : plantList){
            // 随机温度在[1,150)之间
            temperature = random.nextInt(150)+1;
            SysRecord sysRecord = new SysRecord();
            sysRecord.setTemperature(String.valueOf(temperature));
            // 随机压力在[100,300)之间
            pressure = random.nextInt(200)+100;
            sysRecord.setPressure(String.valueOf(pressure));
            sysRecord.setPlantId(plant.getId());
            sysRecord.setRecordTime(date);
            recordList.add(sysRecord);
        }
        return sysRecordMapper.emulateInsertRecord(recordList);
    }

    /**
     * 查询巡检记录，巡检人员上传现场数据
     * 
     * @param id 巡检记录，巡检人员上传现场数据ID
     * @return 巡检记录，巡检人员上传现场数据
     */
    @Override
    public SysRecord selectSysRecordById(Long id)
    {
        return sysRecordMapper.selectSysRecordById(id);
    }

    /**
     * 查询巡检记录，巡检人员上传现场数据列表
     * 
     * @param sysRecord 巡检记录，巡检人员上传现场数据
     * @return 巡检记录，巡检人员上传现场数据
     */
    @Override
    public List<SysRecord> selectSysRecordList(SysRecord sysRecord)
    {
        return sysRecordMapper.selectSysRecordList(sysRecord);
    }

    /**
     * 新增巡检记录，巡检人员上传现场数据
     * 
     * @param sysRecord 巡检记录，巡检人员上传现场数据
     * @return 结果
     */
    @Override
    public int insertSysRecord(SysRecord sysRecord)
    {
        return sysRecordMapper.insertSysRecord(sysRecord);
    }

    /**
     * 修改巡检记录，巡检人员上传现场数据
     * 
     * @param sysRecord 巡检记录，巡检人员上传现场数据
     * @return 结果
     */
    @Override
    public int updateSysRecord(SysRecord sysRecord)
    {
        return sysRecordMapper.updateSysRecord(sysRecord);
    }

    /**
     * 删除巡检记录，巡检人员上传现场数据对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteSysRecordByIds(String ids)
    {
        return sysRecordMapper.deleteSysRecordByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除巡检记录，巡检人员上传现场数据信息
     * 
     * @param id 巡检记录，巡检人员上传现场数据ID
     * @return 结果
     */
    @Override
    public int deleteSysRecordById(Long id)
    {
        return sysRecordMapper.deleteSysRecordById(id);
    }
}
